//
//  BHCompetitionDetailHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/15.
//

import UIKit

class BHCompetitionDetailHeaderView: UIView {

    @IBOutlet weak var banner_coverView: UIView!
    
    @IBOutlet weak var content_view: UIView!
    
    @IBOutlet var iconImgArray: [UIImageView]!
    
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var startTime_label: UILabel!
    
    @IBOutlet weak var area_label: UILabel!
    
    @IBOutlet weak var holder_name_label: UILabel!
    
    @IBOutlet weak var price_label: UILabel!
    
    @IBOutlet weak var game_time_label: UILabel!
    
    @IBOutlet weak var signUp_time_label: UILabel!
    
    @IBOutlet weak var place_label: UILabel!
    
    @IBOutlet weak var apply_label: UILabel!
    
    @IBOutlet weak var applyDetailBtn: UIButton!
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 8
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    static func loadFromXib() -> BHCompetitionDetailHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHCompetitionDetailHeaderView", owner: nil, options: nil)
        let view: BHCompetitionDetailHeaderView = viewArray![0] as! BHCompetitionDetailHeaderView
        return view
    }
    
    
    var model: BHCompetitionDetailModel! {
        didSet {
            self.name_label.text = model.name
            self.startTime_label.text = model.startDate
            self.area_label.text = "\(model.cityName)/\(model.regionName)"
            self.holder_name_label.text = "主办方：\(model.sponsor)"
            if model.feeType == 1 { // 费用类型 0免费 1报名费 2押金
                self.price_label.text = "报名费：\(model.fee)"
            }else if model.feeType == 2 {
                self.price_label.text = "押金：\(model.fee)"
            }else {
                self.price_label.text = "报名费：无需费用"
            }
            
            self.game_time_label.text = "比赛时间：\(model.startTime)至\(model.endTime)"
            self.signUp_time_label.text = "报名截止：\(model.signupEnd)"
            self.place_label.text = "\(model.address)"
            self.apply_label.text = "已报名：\(model.number)人（剩余\(model.remain)个名额）"
            
            for (index,image_view) in self.iconImgArray.enumerated() {
                if index < model.urls.count {
                    image_view.isHidden = false
                    let url = model.urls[index]
                    image_view.sd_setImage(with: URL.init(string: url), completed: nil)
                }else {
                    image_view.isHidden = true
                }
            }
            
            self.bannerView.sourceArray = model!.images
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.content_view.addCorner(conrners: [.topLeft,.topRight], radius: 12)
        for imgView in self.iconImgArray {
            imgView.addLayerCorner(9.5, 0.5, .white)
        }
        
        self.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.bottom()?.equalTo()(self.banner_coverView)
        }
    }
    
    
    static var headerHeight: CGFloat {
        get {
            let bannerHeight: CGFloat = CGFloat(274).radius
            let contentHeight = CGFloat(270.0)
            return (bannerHeight+contentHeight)
        }
    }
    
}
extension BHCompetitionDetailHeaderView: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        if self.model != nil && self.model?.images.count != 0 {
            let model = self.model!.images[indexPath.item%self.model!.images.count]
            cell.bannerImageUrl = model.imageUrl ?? ""
        }
    
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width, height: CGFloat(274).radius)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        if self.model == nil {
            return 0
        }
        return self.model!.images.count
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {

    }
}
